import React from 'react';
import {Button} from 'antd';
import BaseComp from "../Base/BaseComp";
import PropTypes from "prop-types";
import {assign, isFunction, keys, omit} from "lodash-es";
import ComponentType from "../Config/ComponentType";


export default class ButtonComp extends BaseComp {

    static propTypes = {
        ...BaseComp.propTypes,
        text: PropTypes.string,
        type: PropTypes.string,
        size: PropTypes.string,
        ghost: PropTypes.bool,
        shape: PropTypes.string
    };


    moreCommonProps() {
        return [
            ...super.moreCommonProps(),
            'text', 'path', 'targetJavaType'
        ];
    }

    componentDidMount() {
        super.componentDidMount();

        // 将此按钮的实例保存到 appins 中，用于在被需要时手动控制此组件的逻辑
        const {appins, id, customprops} = this.props;
        if (appins && id && isFunction(appins.setBtnInsById)) {
            appins.setBtnInsById(id, this, customprops);
        }
    }

    /**
     * eventConfig 是 afc 中用来控制按钮操作的配置。
     * 这个方法会在 afc 的应用中被手动调用
     * @param eventConfig
     */
    setEventConfig(eventConfig) {
        // eventConfig:
        // {
        //     "id": "saveButton",
        //     "enabled": true,
        //     "handler": "saveHandler",
        //     "eventName": "click",
        //     "shortcutKey": "CTRL+S"
        // }
        const {enabled = true} = eventConfig || {};
        this.setCompProps({disabled: !enabled});
    }

    handleClick(e) {
        const {appins, id, componentType, path} = this.props;

        if (appins && id) {
            const customprops = this.props.customprops || {};

            // 上下文按钮
            if (ComponentType.CONTEXT_BUTTON === componentType && isFunction(appins.handleClickCtxBtn)) {
                appins.handleClickCtxBtn(e, id, {
                    ...customprops, path, ins: this
                });

            } else if (isFunction(appins.handleClickBtn)) {
                appins.handleClickBtn(e, id, {
                    ...customprops, ins: this
                });
            }

        } else {
            // console.error('【ButtonComp】appins 或 id 属性为空', appins, id);
            this.runPropsFunc('onClick')(e);
        }
    }

    getMoreBtnProps() {
        const props = this.getProps();
        const finalChildren = this.renderBtnText();
        const fStyleO = assign({}, this.props.style, this.state.compMoreProps.style);
        const moreProps = {
            ...omit(props, this.commonProps),
            children: finalChildren,
            style: fStyleO
        };

        if (moreProps.loading) {
            moreProps.style = assign(moreProps.style, {
                paddingLeft: 2, paddingRight: 2
            });
        }
        return moreProps;
    }

    renderBtnText() {
        const {text, children} = this.getProps();
        return children || text || null;
    }

    mainRender(context) {
        const moreProps = this.getMoreBtnProps();
        return <Button
            {...moreProps}
            onClick={this.handleClick.bind(this)}
        />
    };

}